package cn.gxufe.reliable.message.producer.controller;

import cn.gxufe.reliable.message.api.aware.CompensableMessageAware;
import cn.gxufe.reliable.message.api.service.CompensableMessage;
import cn.gxufe.reliable.message.common.Message;
import cn.gxufe.reliable.message.producer.clients.RMSClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author yingjie.rong
 **/
@RestController
@RequestMapping("/")
public class TestController implements CompensableMessageAware {

    private CompensableMessage compensableMessage;

    @Autowired
    private RMSClient reliableMessageClient;


    @RequestMapping("/test")
    @Transactional(rollbackFor = Throwable.class)
    public String test(@RequestParam("name") String name){
        Message message = new Message();
        message.setKey("test_key_"+ UUID.randomUUID().toString().replace("-",""));
        message.setHeader("header01");
        message.setData("{TestController}");
        compensableMessage.set("name",name);
        reliableMessageClient.prepare(message);
        return "ok";
    }

    @Override
    public void setCompensableMessage(CompensableMessage compensableMessage) {
        this.compensableMessage = compensableMessage;
    }
}
